package com.owc.common.sms;

import com.alibaba.fastjson.JSON;
import com.owc.common.tool.HttpUtils;
import com.owc.domain.SmsSendRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Author xiaom
 * @Date 2019/12/2 14:02
 * @Version 1.0.0
 * @Description <>
 **/
@Component
@ConfigurationProperties(prefix = "sms")
@Slf4j
public class SmsUtils implements Serializable {

    private static final long serialVersionUID = -4588923720114655613L;


    /**
     * 发送国内短信
     *
     */
    public void send(String phone, String msg) throws UnsupportedEncodingException {
        String report = "true";

        msg = validatecount.replace("######",msg);

        SmsSendRequest smsSingleRequest = new SmsSendRequest(this.getAccount(), this.getPassword(), msg, phone, report);

        String requestJson = JSON.toJSONString(smsSingleRequest);

        log.info("before request string is: " + requestJson);

        String realUrl = url.replace("{1}",getAccount()).replace("{2}",getPassword()).replace("{3}",phone).replace("{4}", msg);

        log.info("account: {} password: {} url: {} ", this.getAccount(), this.getPassword(), realUrl);

        ResponseEntity<Object> response = HttpUtils.getForEntity(realUrl,Object.class);

        log.info("response after request result is :" + response);
    }

    private String url;

    private String account;

    private String password;

    private String outurl;

    private String outurlbatch;

    private String outaccount;

    private String outpassword;

    private String validatecount;

    public SmsUtils() {
    }

    public SmsUtils(SmsUtils ss) {
        this.url = ss.url;
        this.account = ss.account;
        this.password = ss.password;

        this.outurl = ss.outurl;
        this.outurlbatch = ss.outurlbatch;
        this.outaccount = ss.outaccount;
        this.outpassword = ss.outpassword;
        this.validatecount = ss.getValidatecount();
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getOuturl() {
        return outurl;
    }

    public void setOuturl(String outurl) {
        this.outurl = outurl;
    }

    public String getOuturlbatch() {
        return outurlbatch;
    }

    public void setOuturlbatch(String outurlbatch) {
        this.outurlbatch = outurlbatch;
    }

    public String getOutaccount() {
        return outaccount;
    }

    public void setOutaccount(String outaccount) {
        this.outaccount = outaccount;
    }

    public String getOutpassword() {
        return outpassword;
    }

    public void setOutpassword(String outpassword) {
        this.outpassword = outpassword;
    }

    public String getValidatecount() {
        return validatecount;
    }

    public void setValidatecount(String validatecount) {
        this.validatecount = validatecount;
    }
}
